package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringAddressDao;
import com.catering.entity.CateringAddress;
import com.catering.service.CateringAddressService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringAddressServiceImpl extends ServiceImpl<CateringAddressDao, CateringAddress> implements CateringAddressService {
	
	@Autowired
	private CateringAddressDao cateringAddressDao;
	
	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"t1");
			List<CateringAddress> list = new ArrayList<CateringAddress>();
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringAddress> page = new Page<>(Integer.valueOf(map.get("page")),Integer.valueOf(map.get("limit")));
				IPage<CateringAddress> pageList = cateringAddressDao.selectPageList(page, wrapper);
				Integer count = cateringAddressDao.selectPageCount(wrapper);
				list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				list = cateringAddressDao.selectNoPageList(wrapper);
				return ResultData.success(list);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringAddress vo = JSON.parseObject(JSON.toJSONString(map), CateringAddress.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			CateringAddress address = cateringAddressDao.selectOne(new LambdaQueryWrapper<CateringAddress>().eq(CateringAddress::getUserId,vo.getUserId()).eq(CateringAddress::getIsDefault,1));
			if(address == null) vo.setIsDefault(1);
			cateringAddressDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringAddress vo = JSON.parseObject(JSON.toJSONString(map), CateringAddress.class);
			if(CommonUtils.checkNoNull(vo.getIsDefault()) && vo.getIsDefault() == 1) {
				CateringAddress cateringAddress = cateringAddressDao.selectById(vo.getSysId());
				CateringAddress address = new CateringAddress();
				address.setIsDefault(0);
				cateringAddressDao.update(address,new LambdaQueryWrapper<CateringAddress>().eq(CateringAddress::getUserId,cateringAddress.getUserId()));
			}
			cateringAddressDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,String> map) {
		try {
			CateringAddress vo = JSON.parseObject(JSON.toJSONString(map), CateringAddress.class);
			List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
			cateringAddressDao.deleteBatchIds(list);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
